package com.nenglai.admin.modular.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nenglai.admin.handler.RequestHolder;
import com.nenglai.admin.modular.report.vo.ReportDownloadVO;
import com.nenglai.admin.modular.system.enums.AdminType;
import com.nenglai.admin.util.COSClientUtil;
import com.nenglai.common.crud.EditorBody;
import com.nenglai.common.crud.FormBody;
import com.nenglai.common.crud.RemoveBody;
import com.nenglai.common.crud.SearchBody;
import com.nenglai.common.resource.Datalist;
import com.nenglai.common.resource.ResourceService;
import com.nenglai.common.response.ErrorCode;
import com.nenglai.common.response.ResponseException;
import com.nenglai.common.response.ValidateException;
import com.nenglai.common.vo.SearchOneVO;
import com.nenglai.common.vo.SearchVO;
import com.nenglai.model.report.entity.Download;
import com.nenglai.model.report.service.IDownloadService;
import com.nenglai.model.system.entity.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;

/**
 * 报告下载
 *
 * @author kongdehui
 * @date 2021/06/26 11:50
 **/
@Slf4j
@Service
public class ReportDownloadService implements ResourceService<ReportDownloadVO> {

    @Resource
    IDownloadService downloadService;

    @Resource
    COSClientUtil cosClientUtil;

    @Override
    public String resourceName() {
        return "report-download";
    }

    @Override
    @Datalist(entity = Download.class, vo = ReportDownloadVO.class)
    public SearchVO<Download> search(SearchBody body) {
        QueryWrapper<Download> query = body.query(new QueryWrapper<>(), Download.class);
        Page<Download> page = downloadService.page(new Page<>(body.getPage(), body.getPagesize()), query);
        return new SearchVO<>(page.getRecords(), page.getTotal());
    }

    @Override
    public SearchOneVO<ReportDownloadVO> searchOne(SearchBody body) {
        return null;
    }

    @Override
    public Boolean delete(RemoveBody body) {
        QueryWrapper<Download> query = body.query(new QueryWrapper<>(), Download.class, true);
        Download report = downloadService.getOne(query);
        if (report == null) {
            throw new ResponseException(ErrorCode.OBJECT_DOES_NOT_EXIST);
        }

        Admin user = (Admin) RequestHolder.getRequest().getAttribute("user");
        // 超级管理员可删除任意报告
        if (!user.getType().equals(AdminType.TYPE_SUPER.getCode()) && !user.getId().equals(report.getUid())) {
            // 非超管只能删除自己的报告
            throw new ResponseException(ErrorCode.NO_PERMISSION_TO_DELETE_FROM_OTHERS);
        }

        cosClientUtil.deleteObject(report.getDownload());
        /*File file = new File(report.getDownload());
        file.delete();*/
        return downloadService.remove(query);
    }

    @Override
    public Object add(FormBody body) throws ValidateException {
        return null;
    }

    @Override
    public Object update(FormBody body) throws ValidateException {
        return null;
    }

    @Override
    public Boolean edit(EditorBody body) throws ValidateException {
        UpdateWrapper<Download> update = body.query(new UpdateWrapper<>(), Download.class, true);
        return downloadService.update(update);
    }

    @Override
    public Object configs() {
        return new HashMap<>();
    }
}
